package com.springapp.mvc.controller;

import com.springapp.mvc.dao.ParkMapper;
import com.springapp.mvc.dao.UserMapper;
import com.springapp.mvc.model.Park;
import com.springapp.mvc.model.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;

@Controller
@RequestMapping("/")
public class HelloController {
	public SqlSession sqlSession=null;

	public void getSqlSession(){
		try{
			if(sqlSession==null) {
				String resource = "com/springapp/mvc/applicationContext-mybatis.xml";
				InputStream ins = Resources.getResourceAsStream(resource);
				SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ins);
				sqlSession = sqlSessionFactory.openSession();
			}
		}catch (IOException e){
			e.printStackTrace();
		}
	}

	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
		getSqlSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		ParkMapper parkMapper=sqlSession.getMapper(ParkMapper.class);
		User user=userMapper.selectByPrimaryKey(60);
		Park park=parkMapper.selectByPrimaryKey(141);
		System.out.println("结果"+user.getRealfullname());
		model.addAttribute("user", user);
		model.addAttribute("park", park);
		return "hello";
	}
}